﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace c_入门最终作品
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 控制台基础设置  直接打开控制台设置即可
            int w = 50;
            int h = 30; 
            
            Console.CursorVisible = false;    //将光标隐藏


            #endregion
            #region 场景的设置
            int nowSceneID= 1;
            string gameoverInfo = "";
            while (true)
            {
               

                switch (nowSceneID)
                {
                    #region 开始场景逻辑实现
                    case 1:      // 开始场景
                    Console.Clear();
                    bool isQuitWhile = false;                      //用一个标识符表示，用来出来while循环内部的switch语句 非常重要
                    int nowChoice = 0;
                    Console.SetCursorPosition(50 / 2 - 7, 8);
                    Console.WriteLine("唐老师营救公主");
                    while (true)   //检测内容，并检测输入
                    {


                        Console.SetCursorPosition(50 / 2 - 4, 13);
                        Console.ForegroundColor = nowChoice == 0 ? ConsoleColor.Red : ConsoleColor.White;
                        Console.WriteLine("开始游戏");

                        Console.SetCursorPosition(50 / 2 - 4, 15);
                        Console.ForegroundColor = nowChoice == 1 ? ConsoleColor.Red : ConsoleColor.White;
                        Console.WriteLine("结束游戏");

                        char input = Console.ReadKey(true).KeyChar;



                        switch (input)
                        {
                            case 'w':
                            case 'W':
                                --nowChoice;
                                if (nowChoice < 0)
                                {
                                    nowChoice = 0;
                                }
                                break;
                            case 's':
                            case 'S':
                                ++nowChoice;
                                if (nowChoice > 1)
                                {
                                    nowChoice = 1;
                                }
                                break;
                            case 'j':
                            case 'J':
                                if (nowChoice == 0)
                                {
                                        nowSceneID = 2;
                                        isQuitWhile = true;
                                }
                                else
                                {
                                    Environment.Exit(0);
                                }
                                break;

                        }
                            if(isQuitWhile)
                            {
                                break;
                            }

                    }
                        

                        break;
                    #endregion

                    case 2:     // 游戏场景
                        Console.Clear();
                        
                        bool isFight = false;   //一开始不是战斗状态
                        bool isSave = false; //假设一开始没有拯救成功
                        #region 不变的红墙
                        Console.ForegroundColor = ConsoleColor.Red;
                        for (int i = 0; i < w;i+=1)   //最上边的红墙
                        {
                            Console.SetCursorPosition(i, 0);
                           
                            Console.Write("■");   //注意没有Line
                            Console.SetCursorPosition(i, h-1);

                            Console.Write("■");   //注意没有Line
                            Console.SetCursorPosition(i, h - 6);

                            Console.Write("■");   //注意没有Line
                        }
                        
                        for (int i = 0; i < h; i++)    //第一列的红墙
                        {
                            Console.SetCursorPosition(0, i);

                            Console.Write("■");   //注意没有Line
                            Console.SetCursorPosition(w - 1, i);

                            Console.Write("■");   //注意没有Line
                        }

                        #endregion
                       
                        int boxX = 24;   //boss
                        int boxY = 15;
                        int boxATKmin = 8;
                        int boxATKmax = 12;
                        int boxHp = 100;
                        string  boxIcon = "■";
                        //申明一个自定义变量
                        ConsoleColor boxColor = ConsoleColor.Green;
                        //在画布上生成一个玩家
                        int playerX = 4;   //玩家
                        int playerY = 5;
                        int playerATKmin = 8;
                        int playerATKmax = 12;
                        int playerHp = 100;
                        string playerIcon = "●";
                        ConsoleColor playerColor = ConsoleColor.Yellow;
                        int princeX = 24;  //公主
                        int princeY = 5;
                        string princeIcon = "★";
                        ConsoleColor pinceColor = ConsoleColor.Green;

                        while (true)  //绘制boss图标
                        {
                            #region boss属性相关
                            if (boxHp > 0)
                            {
                                Console.SetCursorPosition(boxX, boxY);
                                Console.ForegroundColor = boxColor;
                                Console.WriteLine(boxIcon);
                            }
                            else
                            {
                                #region 公主相关
                                Console.SetCursorPosition(princeX, princeY);
                                Console.ForegroundColor = pinceColor;
                                Console.WriteLine(princeIcon);
                                #endregion
                                
                            }

                            #endregion
                            #region  玩家属性相关
                            Console.SetCursorPosition(playerX, playerY);
                            Console.ForegroundColor = playerColor;
                            Console.WriteLine(playerIcon);
                            char Input = Console.ReadKey(true).KeyChar;
                            #endregion
                           
                            if (isFight)   
                            {
                                if (playerHp <=0)
                                {
                                    nowSceneID = 3;
                                    gameoverInfo = "挑战失败";
                                    break;
                                    //失败
                                }
                                else if(boxHp<=0)
                                {
                                    //去营救公主
                                    //擦除boss
                                    Console.SetCursorPosition(boxX, boxY);
                                    Console.WriteLine(" ");
                                    isFight = false;    
                                }
                                else
                                {
                                    Random r = new Random();
                                    int atk = r.Next(playerATKmin, playerATKmax);
                                    boxHp -= atk;
                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.SetCursorPosition(2, h - 4);
                                    Console.WriteLine("                                               ");
                                    Console.SetCursorPosition(2, h - 4);
                                    Console.Write("玩家对怪兽造成{0}伤害，怪兽当前血量为{1}", atk, boxHp);
                                    if (boxHp > 0)
                                    {
                                        Random s = new Random();
                                        atk = s.Next(boxATKmin, boxATKmax);
                                        playerHp -= atk;
                                        Console.ForegroundColor = ConsoleColor.Yellow;
                                        Console.SetCursorPosition(2, h - 3);
                                        Console.WriteLine("                                             ");

                                        if (playerHp <= 0)
                                        {

                                            Console.WriteLine("很遗憾，您未能通过boss试炼，战败了");
                                        }
                                        else
                                        {
                                            Console.SetCursorPosition(2, h - 3);
                                            Console.Write("怪兽对玩家造成{0}伤害，玩家当前血量为{1}", atk, playerHp);
                                        }
                                    }
                                    else
                                    {
                                        Console.SetCursorPosition(2, h - 5);
                                        Console.WriteLine("                                          ");
                                        Console.SetCursorPosition(2, h - 4);
                                        Console.WriteLine("                                         ");
                                        Console.SetCursorPosition(2, h - 3);
                                        Console.WriteLine("                                            ");
                                        Console.SetCursorPosition(2, h - 5);
                                        Console.WriteLine("恭喜你，成功打败boss，请营救公主，按j键继续");
                                        //解除战斗状态
                                    }
                                }
                               
                               
                                
                                    
                                    
                                


                            }
                            else
                            {
                                Console.SetCursorPosition(playerX, playerY);
                                Console.Write(" ");

                                switch (Input)
                                {
                                    case 'w':
                                    case 'W':
                                        playerY--;
                                        if (playerY < 1)
                                        {
                                            playerY = 1;
                                        }
                                        else if (playerX == boxX && playerY == boxY && boxHp > 0)
                                        {
                                            playerY++;
                                        }
                                        
                                        else if (playerX == princeX&&playerY == princeY&&boxHp<=0)
                                        {
                                            playerY++;
                                        }
                                        break;
                                    case 's':
                                    case 'S':
                                        playerY++;
                                        if (playerY > h - 7)
                                        {
                                            playerY = h - 7;
                                        }
                                        else if (playerX == boxX && playerY == boxY && boxHp > 0)
                                        {
                                            playerY--;
                                        }
                                        else if (playerX == princeX && playerY == princeY && boxHp <= 0)
                                        {
                                            playerY--;
                                        }
                                            break;
                                    case 'a':
                                    case 'A':
                                        playerX -= 1;
                                        if (playerX < 1)
                                        {
                                            playerX = 1;
                                        }
                                        else if (playerX == boxX && playerY == boxY && boxHp > 0)
                                        {
                                            playerX += 1;
                                        }
                                        else if (playerX == princeX && playerY == princeY && boxHp <= 0)
                                        {
                                            playerX++;
                                        }
                                            break;
                                    case 'd':
                                    case 'D':
                                        playerX += 1;
                                        if (playerX > w - 2)
                                        {
                                            playerX = w - 2;
                                        }
                                        else if (playerX == boxX && playerY == boxY && boxHp > 0)
                                        {
                                            playerX -= 1;
                                        }
                                        else if (playerX == princeX && playerY == princeY && boxHp <= 0)
                                        {
                                            
                                            playerX -= 1;
                                        }
                                        break;
                                    //开始战斗
                                    case 'j':
                                    case 'J':
                                        if ((playerX == boxX && playerY - 1 == boxY || playerX == boxX && playerY + 1 == boxY || playerY == boxY && playerX - 1 == boxX || playerY == boxY && playerX + 1 == boxX) && boxHp > 0)
                                        {
                                            isFight = true; 
                                            //如果满足条件，则可以开始战斗
                                            Console.SetCursorPosition(2, h - 5);
                                            Console.ForegroundColor = ConsoleColor.White;
                                            Console.WriteLine("按j键盘可以开始和boss战斗了");
                                            Console.SetCursorPosition(2, h - 3);
                                            Console.WriteLine("当前boss的血量为{0}", boxHp);
                                            Console.SetCursorPosition(2, h - 4);
                                            Console.WriteLine("当前玩家的血量为{0}", playerHp);
                                        }
                                        else if ( ((playerX == princeX && playerY - 1 == princeY || playerX == princeX && playerY + 1 == princeY || playerY == princeY && playerX - 1 == princeX || playerY == boxY && playerX + 1 == princeX) && boxHp <= 0))
                                        {
                                            //改变场景id跳出循环
                                            
                                            isSave = true;
                                        }
                                        break;
                                }
                                if(isSave)
                                {
                                    nowSceneID = 3;
                                    gameoverInfo = "挑战成功";
                                    break;   //break和while配对
                                }
                            }
                            
                           
                        }
                        break;
                    case 3:     //结束场景
                        Console.Clear();
                        Console.SetCursorPosition(w / 2 - 4, 5);
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine("Game Over");
                        Console.SetCursorPosition(w / 2 - 4, 7);
                        Console.Write(gameoverInfo);
                        int now = 0;
                        
                        while (true)
                        {
                            bool isQuit = false;
                            Console.ForegroundColor = now == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.SetCursorPosition(w / 2 - 6, 9);
                            Console.Write("回到开始界面");
                            Console.ForegroundColor = now == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.SetCursorPosition(w / 2 - 4, 10);
                            Console.Write("结束游戏");
                            char c = Console.ReadKey(true).KeyChar;
                            switch(c)
                            {
                                case 'w':
                                case 'W':
                                    now--;
                                    if(now<0)
                                    {
                                        now = 0;
                                    }
                                    break;
                                case 's':
                                case 'S':

                                    now++;
                                    if (now > 1)
                                    {
                                        now = 1;
                                    }
                                    break;
                                case 'j':
                                case 'J':
                                    if( now == 0  )
                                    {
                                        isQuit = true;  
                                        nowSceneID = 1;
                                    }
                                    else
                                    {
                                        Environment.Exit(0);    
                                    }
                                    break; 
                            }
                            if (isQuit)  //跳出场景三的while循环
                            {
                                break;
                            }
                        }
                        
                    break;
                }
            }
            
            #endregion
        }
    }
}
